package vista;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import constants.Constants;
/**
 * this class will create a panel for the conversation of the players 
 * about features in their cards 
 * @author Jose Luis Garcia && Alvaro Blazquez Checa
 *
 */
public class MeetingPanel extends JFrame implements ActionListener{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private JTextField name;
		private JComboBox<String> combo1, silenceVoteOptions,questionType,whereToAsk;
		private JPanel charactheristicsOptions;
		private List<ButtonGroup> buttonGroup;
		private HashMap<String,List<JRadioButton> > radioButtonsList;
		private JButton ask, sendSilenceVote,sendQuestion;
		private JLabel label1,label2,label3,label4, askedPlayer,title1,title2;
		private JList<String> monkNamesList;
		private JScrollPane listContainer;
		private String playerAnswering;
		/**
		 * this will construct the aspects for this panel
		 * @param bounds the size of the meeting panel
		 * @param questions number of features to be added
		 * @param monkNames number of monk names to be added
		 * @param backGroundIcon the background for the panel
		 * @param buttonIcon the button Icon for the panel
		 */
		@SuppressWarnings("unchecked")
		public MeetingPanel(Rectangle bounds,List<String> questions, List<String> monkNames,Icon backGroundIcon,Icon buttonIcon) {
			
			setBounds(bounds);
			setLayout(null);
			setUndecorated(true);
			
			Border outsideBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
			Border insideBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
			
			((JComponent) getContentPane()).setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
			
			Font font = new Font( "SketchFlow Print",Font.BOLD,22);
			Font font2 = new Font( "SketchFlow Print",Font.BOLD,14);
			
			JLabel title = new JLabel();
			title.setFont(font);
			title.setText("ENCUENTRO");
			title.setForeground(Color.white);
			title.setSize(150,20);
			title.setLocation(130,20);
			
			JLabel background=new JLabel(backGroundIcon);
			background.setLayout(null);
			background.setBounds(3,3,394,394);
			background.setVisible(true);
			
			label1 = new JLabel();
			label1.setFont(font2);
			label1.setText("Preguntar a:");
			label1.setForeground(Color.WHITE);
			label1.setSize(120,20);
			label1.setLocation(30,60);
			
			label2 = new JLabel();
			label2.setFont(font2);
			label2.setText("Voto de silencio?:");
			label2.setOpaque(false);
			label2.setForeground(Color.WHITE);
			label2.setSize(150,20);
			label2.setLocation(30,100);
			
			label3 = new JLabel();
			label3.setFont(font2);
			label3.setOpaque(false);
			label3.setForeground(Color.WHITE);
			//label3.setBackground(Color.BLACK);
			label3.setSize(170,20);
			label3.setLocation(170,160);
			
			label4 = new JLabel();
			label4.setFont(font2);
			label4.setOpaque(false);
			label4.setForeground(Color.WHITE);
			//label4.setBackground(Color.BLACK);
			label4.setSize(250,20);
			label4.setLocation(70,60);
			
			askedPlayer = new JLabel();
			askedPlayer.setFont(font2);
			askedPlayer.setOpaque(true);
			//label4.setBackground(Color.BLACK);
			askedPlayer.setSize(160,20);
			askedPlayer.setLocation(200,60);
			
			combo1=new JComboBox<String>();
		    combo1.setBounds(200,60,160,20);
		    
		    
		    title1 = new JLabel("Tipo pregunta");
		    title1.setForeground(Color.white);
		    title1.setBounds(50, 80, 120, 20);
		    add(title1);
		    
		    questionType = new JComboBox<String>();
		    questionType.setBounds(30, 100, 160, 20);
		    questionType.addItem("Caracteristicas");
		    questionType.addItem("Monjes");
		    questionType.addActionListener(this);
		    
		    title2 = new JLabel("Donde preguntar");
		    title2.setForeground(Color.white);
		    title2.setBounds(250, 80, 120, 20);
		    add(title2);
		    
		    whereToAsk = new JComboBox<String>();
		    whereToAsk.setBounds(230, 100, 160, 20);
		    whereToAsk.addItem("en la mano");
		    whereToAsk.addItem("tachado hoja sospechpsos");
		    whereToAsk.addActionListener(this);
		    
		    silenceVoteOptions = new JComboBox<String>();
		    silenceVoteOptions = new JComboBox<String>();
		    silenceVoteOptions.addItem("SI");
		    silenceVoteOptions.addItem("NO");
		    silenceVoteOptions.setBounds(200,100,160,20);
		    
		    
		    ask = new JButton("PREGUNTAR");
		    ask.setName("PREGUNTAR");
		    ask.setFont(font2);
		    ask.addActionListener(MainGUI.getInstance());
		    ask.setBounds(130, 100, 130, 30);
		    ask.setLayout(null);
		    JLabel buttonText4 = new JLabel();
		    buttonText4.setText("Enviar");
		    buttonText4.setFont(Constants.NORMAL_FONT);
		    buttonText4.setForeground(Color.white);
		    buttonText4.setLayout(null);
		    buttonText4.setBounds(50, 0,130, 30);
		    ask.add(buttonText4);
		    
		    
		    sendSilenceVote = new JButton("ENVIAR");
		    sendSilenceVote.setIcon(buttonIcon);
		    sendSilenceVote.setLayout(null);
		    JLabel buttonText = new JLabel();
		    buttonText.setText("Enviar");
		    buttonText.setFont(Constants.NORMAL_FONT);
		    buttonText.setForeground(Color.white);
		    buttonText.setLayout(null);
		    buttonText.setBounds(50, 0,130, 30);
		    sendSilenceVote.add(buttonText);
		    
		    
		    sendSilenceVote.setName("ENVIAR VOTO");
		    sendSilenceVote.setFont(font2);
		    sendSilenceVote.addActionListener(MainGUI.getInstance());
		    sendSilenceVote.setBounds(130, 140, 130, 30);
		  
		   
		    sendQuestion = new JButton("ENVIAR PREGUNTA");
		    sendQuestion.setName("ENVIAR PREGUNTA");
		    sendQuestion.setIcon(buttonIcon);
		    sendQuestion.setLayout(null);
		    JLabel buttonText1 = new JLabel();
		    buttonText1.setText("Enviar");
		    buttonText1.setFont(Constants.NORMAL_FONT);
		    buttonText1.setForeground(Color.white);
		    buttonText1.setLayout(null);
		    buttonText1.setBounds(50, 0, 130, 30);
		    sendQuestion.add(buttonText1);
		    
		    sendQuestion.setFont(font2);
		    sendQuestion.addActionListener(MainGUI.getInstance());
		    sendQuestion.setBounds(130, 140, 130, 30);
		    
		    monkNamesList = new JList(monkNames.toArray());
		    
		    listContainer = new JScrollPane(monkNamesList);
		    listContainer.setBounds(10, 180, 380, 200);
		    //Seleccion de caracteristicas

			setQuestionPanel(questions);
			
		    add(label1);
		    add(label2);
		    add(label3);
		    add(label4);
		    add(askedPlayer);
		    add(title);
			add(combo1);
			add(whereToAsk);
			add(questionType);
			add(silenceVoteOptions);
			add(ask);
			add(sendSilenceVote);
			add(sendQuestion);
			add(charactheristicsOptions);
			add(listContainer);
			add(background);
			
			//setAlwaysOnTop(true);
		}
		/**
		 * this will set the players to ask in the combo box
		 * @param players
		 */
		public void setValues(List<String> players) {
			combo1.removeAllItems();
	        for(String str : players) 
	        	combo1.addItem(str);
		}
		/**
		 * this will return the previously selection of the player 
		 * to ask
		 * @return a player selected
		 */
		public String getPlayerToAsk() {
			
			playerAnswering = (String) combo1.getSelectedItem();
			return combo1.getItemAt(combo1.getSelectedIndex());
			
		}
		/**
		 * this returns what player answer
		 * @return
		 */
		public String getPlayerAnswering() {
			return playerAnswering;
		}
		/**
		 * this will set an answer for the previous question in the game
		 * @param playerAnswering
		 */
		public void setPlayerAnswering(String playerAnswering) {
			this.playerAnswering = playerAnswering;
		}
		/**
		 * this will return if the other player decided to make a silence vote
		 * @return
		 */
		public String getSilenceVoteAnswer() {
			
			return silenceVoteOptions.getItemAt(silenceVoteOptions.getSelectedIndex());
					
		}
		/**
		 * this will handle what to do if the other user answer no to the 
		 * silence vote
		 * @param name user that make silence vote
		 */
		public void negativeAnswer(String name) {
			
			label3.setText(name + " hizo voto de silencio");
			label3.setVisible(true);
			JOptionPane.showMessageDialog(this, name + " hizo voto de silencio", "VOTO DE SILENCIO", JOptionPane.DEFAULT_OPTION);
			this.setVisible(false);
			
		}
		/**
		 * this will handle what to do if the other user answer yes to the 
		 * silence vote
		 */
		public void answerMode(String playerAsking) {
			
			label1.setVisible(false);
			label3.setVisible(false);
			title1.setVisible(false);
			title2.setVisible(false);
			askedPlayer.setVisible(false);
			combo1.setVisible(false);
			ask.setVisible(false);
			charactheristicsOptions.setVisible(false);
			listContainer.setVisible(false);
			sendQuestion.setVisible(false);
			

			questionType.setVisible(false);
			whereToAsk.setVisible(false);
			
			sendSilenceVote.setVisible(true);
			silenceVoteOptions.setVisible(true);
			label2.setVisible(true);
			label4.setText(playerAsking + " te quiere preguntar");
			label4.setVisible(true);
			
		}
		/**
		 * this will add the features to construct a question of the features for the user 
		 */
		public void askMode() {
			
			label1.setVisible(true);
			combo1.setVisible(true);
			ask.setVisible(true);
			
			charactheristicsOptions.setVisible(false);
			title1.setVisible(false);
			title2.setVisible(false);
			sendSilenceVote.setVisible(false);
			silenceVoteOptions.setVisible(false);
			label2.setVisible(false);
			label4.setVisible(false);
			askedPlayer.setVisible(false);
			listContainer.setVisible(false);
			sendQuestion.setVisible(false);
			label3.setVisible(false);
			
			questionType.setVisible(false);
			whereToAsk.setVisible(false);
			
		}
		/**
		 * this will add the features to construct a question of the monks for the user 
		 */
		public void questionsMode(String askedPlayerName) {
			
			askedPlayer.setText(askedPlayerName);
			
			ask.setVisible(false);
			combo1.setVisible(false);
			label3.setVisible(false);
			listContainer.setVisible(false);
			label1.setVisible(false);
			combo1.setVisible(false);
			ask.setVisible(false);
			silenceVoteOptions.setVisible(false);
			label2.setVisible(false);
			label3.setVisible(false);
			charactheristicsOptions.setVisible(false);
			sendSilenceVote.setVisible(false);
			
			questionType.setVisible(true);
			whereToAsk.setVisible(true);
			askedPlayer.setVisible(true);
			title1.setVisible(true);
			title2.setVisible(true);
			sendQuestion.setVisible(true);
			
			
		}
		/**
		 * this will reset our combos selections
		 */
		public void uncheckAll() {
			 for(ButtonGroup group: buttonGroup) {
				 group.clearSelection();
			 }
			 monkNamesList.clearSelection();
		}
		/**
		 * this will construc the question with the currents values
		 * @param list list of features
		 * @return true if the curren question is ok
		 */
		public boolean getQuestionValue(List<String> list){
			
			boolean questionOk = false;
			
			String type = (String) questionType.getSelectedItem();
			String where = (String) whereToAsk.getSelectedItem();
			
			list.add(playerAnswering);		
			
			list.add(type);
			list.add(where);

			list.add("|");
			
			if(type.equalsIgnoreCase("Caracteristicas")) {
				
				Set<String> keys = radioButtonsList.keySet();
				int count = 0;
					
				for(String characteristicName : keys) {
					List<JRadioButton> buttonGroup = radioButtonsList.get(characteristicName);
					for (JRadioButton pibot : buttonGroup)
					{
						if (pibot.isSelected()){
							list.add(characteristicName);
							list.add(pibot.getText());
							list.add("|");
							count++;	
					
						} 
					}
					
				}
				if(count>1) {
					questionOk = true;
				}
				
			}
			else if(type.equalsIgnoreCase("Monjes")) {
				
				if(monkNamesList.getSelectedValue()!= null) {
					String monkName = monkNamesList.getSelectedValue();
					list.add(monkName);
					questionOk = true;
				}
				
			}
			
			return questionOk;
		}
		/**
		 * this method instanciate the features for make a question 
		 * @param questions types of features in the combo box
		 */
		private void setQuestionPanel(List<String> questions) {

			charactheristicsOptions = new JPanel();
			charactheristicsOptions.setBounds(10, 180, 380, 200);
			charactheristicsOptions.setBackground(Color.CYAN);

			charactheristicsOptions.setLayout(null);

			radioButtonsList = new HashMap<String,List<JRadioButton>>();

			Iterator<String> itr = questions.iterator();

			
			int posX=10,posY=20,xtitleSize = 70,xsize = 85,ysize= 20;
			Font f = new Font( "SketchFlow Print",Font.PLAIN,9);
			buttonGroup = new ArrayList<ButtonGroup>();
			while(itr.hasNext()) {

				ButtonGroup values = new ButtonGroup();
				buttonGroup.add(values);
				List<JRadioButton> listIterable = new ArrayList<JRadioButton>();
				
				JLabel title = new JLabel(itr.next());
				title.setBounds(posX, posY, xtitleSize, ysize);
				title.setBackground(Color.DARK_GRAY);
				
				radioButtonsList.put(title.getText(), listIterable);
				
				charactheristicsOptions.add(title);
				
				String value = itr.next();
				posX+=70;
				while(itr.hasNext() && !value.equals("|")) {
					
					JRadioButton button = new JRadioButton(value);
					button.setFont(f);
					button.setBounds(posX, posY, xsize, ysize);
					
					listIterable.add(button);
					
					
					values.add(button);
					charactheristicsOptions.add(button);
					value = itr.next();
					posX+=95;
				}	
				
				posX = 10;
				posY+=30;

			}

		}	
		
		public void painComponent(Graphics g) {
			repaint();
		}
		

		
		
		@Override
		public void actionPerformed(ActionEvent e) {
			
			JComboBox<String> comboBox = (JComboBox<String>) e.getSource();
			
			String value = (String) comboBox.getSelectedItem();
			
			if(value.equalsIgnoreCase("Caracteristicas")) {
				charactheristicsOptions.setVisible(true);
				listContainer.setVisible(false);
			}
			else if(value.equalsIgnoreCase("Monjes")) {
				charactheristicsOptions.setVisible(false);
				listContainer.setVisible(true);
			}
			
		}
		

}
